package com.xin.di.uav.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.xin.di.uav.po.Bill;
import com.xin.di.uav.user.order.po.PaymentBillPO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author LTQ
 * @since 2023-07-03
 */
@Mapper
public interface BillMapper extends BaseMapper<Bill> {

    @Select({"<script>",
            "select  id, create_time, update_time, order_id, pay_status, user_id, refund_failed_reason, type,\n" +
                    "            tc_waybill_no, price_paid, processed_by from bill  where  tc_waybill_no =#{orderSn} and pay_status=7 and type=1",
            "</script>"})
    Bill selectIncomePending(String orderSn);


    @Select({"<script>",
            "select  id, create_time, update_time, order_id, pay_status, user_id, refund_failed_reason, type,\n" +
                    "            tc_waybill_no, price_paid, processed_by from bill  where  tc_waybill_no =#{orderSn} and pay_status=9 and type=2",
            "</script>"})
    Bill selectRefundPending(String orderSn);

    @Update({
            "<script>",
            "update bill set update_time=#{updateTime},pay_status=#{payStatus}" +
                    "    <if test='otherOrderNo != null'> ,other_order_no = #{otherOrderNo}</if>",
                            " where id=#{id}",
            "</script>"
    })
    int updateStatus(Bill bill);


    @Select({"<script>",
            "select count(1) \n" +
                    "from bill where 1=1 " +
                    "<if test='startTime != null '>" +
                    " AND update_time &gt; #{startTime} AND update_time &lt; #{endTime} </if>",
            "    <if test='orderNo != null and orderNo !=\"\"  '> and  tc_waybill_no = #{orderNo}</if>",
            "</script>"})
    int count(@Param("orderNo") String orderNo, @Param("startTime") Date startTime, @Param("endTime") Date endTime);




    @Select({"<script>",
            "select id, create_time,other_order_no, update_time, order_id, pay_status, user_id, refund_failed_reason, type,\n" +
                    "                  tc_waybill_no, price_paid, processed_by \n" +
                    "from bill where 1=1 " +
                    "<if test='startTime != null '>" +
                    " AND update_time &gt; #{startTime} AND update_time &lt; #{endTime} </if>",
            "    <if test='orderNo != null and orderNo !=\"\"  '> and  tc_waybill_no = #{orderNo}</if>",
            "order by update_time desc   limit #{offset},#{pageSize} ",
            "</script>"})
    List<Bill> selectList(@Param("offset") int offset, @Param("pageSize") int pageSize,
                                   @Param("orderNo") String orderNo, @Param("startTime") Date startTime, @Param("endTime") Date endTime);
}
